package robot

import (
	"strings"
)

// DBCtoSBC 全角转半角.
func DBCtoSBC(s string) string {
	retstr := ""

	for _, i := range s {
		insideCode := i

		if insideCode == 12288 { // nolint: gomnd
			insideCode = 32
		} else {
			insideCode -= 65248
		}

		if insideCode < 32 || insideCode > 126 {
			retstr += string(i)
		} else {
			retstr += string(insideCode)
		}
	}

	return retstr
}

// GetArg 获取参数.
func GetArg(str string) string {
	if str == "" {
		return ""
	}

	r := []rune(str)[0]
	if r > 32 && r < 65 {
		return strings.TrimSpace(str[1:])
	}

	if i := strings.Index(str, " "); i > 0 {
		return strings.TrimSpace(str[i:])
	}

	return ""
}

// GetArgs 获取参数列表.
func GetArgs(str string) []string {
	arg := GetArg(str)
	args := []string{}

	for _, a := range strings.Split(arg, " ") {
		a = strings.TrimSpace(a)
		if a != "" {
			args = append(args, a)
		}
	}

	return args
}
